"""FastAPI dependency helpers."""
from typing import Optional
import os
import redis.asyncio as redis
from .database import AsyncSessionLocal

# Global Redis client
_redis_client: Optional[redis.Redis] = None

async def get_db():
    async with AsyncSessionLocal() as db:
        yield db

async def get_redis() -> Optional[redis.Redis]:
    """Get Redis client if configured."""
    global _redis_client
    
    redis_url = os.getenv("REDIS_URL")
    if not redis_url:
        return None
    
    if _redis_client is None:
        _redis_client = redis.from_url(redis_url, decode_responses=True)
    
    return _redis_client
